package cn.net.cfss.fgbp.face.console.controller;

import cn.net.cfss.fgbp.api.console.inte.ISysRoleAppService;
import cn.net.cfss.fgbp.api.console.vo.SysRoleAppVo;
import cn.net.cfss.fgbp.base.constant.ErrCodeEnum;
import cn.net.cfss.fgbp.base.controller.BaseController;
import cn.net.cfss.fgbp.face.console.util.ReturnUtil;
import cn.net.cfss.fgbp.face.console.vo.BaseParas;
import cn.net.cfss.fgbp.log.influxdb.ApiDto;
import cn.net.cfss.fgbp.log.statistics.IStatsdLogService;
import cn.net.cfss.fgbp.log.statistics.LogDtoFactory;
import cn.net.cfss.fgbp.log.util.LogStandardUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * @author oygq
 * @title: SysRoleAppController
 * @description: TODO
 * @date 2019/4/8 16:16
 */
@RestController
@Slf4j
@RequestMapping("console/roleApp")
public class SysRoleAppController extends BaseController {

    @Autowired
    ISysRoleAppService roleAppService;

    @ApiOperation("角色应用新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String inset(@RequestBody BaseParas paras){
        SysRoleAppVo vo = JSONObject.toJavaObject(paras.getPlain(),SysRoleAppVo.class);
        if(roleAppService.insert(vo)){
            return ReturnUtil.returnSucc();
        }
        return ReturnUtil.returnErr(ErrCodeEnum.ERR);
    }

    @ApiOperation("角色应用修改")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String update(@RequestBody BaseParas paras){
        SysRoleAppVo vo = JSONObject.toJavaObject(paras.getPlain(),SysRoleAppVo.class);
        if(roleAppService.update(vo)){
            return ReturnUtil.returnSucc();
        }
        return ReturnUtil.returnErr(ErrCodeEnum.ERR);
    }

    @ApiOperation("角色菜单删除")
    @RequestMapping(value = "/delete/{roleAppId}", method = RequestMethod.POST)
    public String update(@PathVariable Long roleAppId){
        //构建监控数据
        String trace = UUID.randomUUID().toString();
        if(null == roleAppId){
            return ReturnUtil.returnErr("角色应用为空");
        }
        if(roleAppService.delete(roleAppId)){
            return ReturnUtil.returnSucc();
        }
        return ReturnUtil.returnErr(ErrCodeEnum.ERR);
    }

    @ApiOperation("角色菜单批量删除")
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    public String deleteBatch(@RequestBody BaseParas paras){
        List<Long> roleAppIds = (List<Long>) paras.getPlain().get("roleAppIds");
        if(roleAppService.deleteBatch(roleAppIds.toArray(new Long[roleAppIds.size()]))){
            return ReturnUtil.returnSucc();
        }
        return ReturnUtil.returnErr(ErrCodeEnum.ERR);
    }

    @ApiOperation("根据角色id查询应用")
    @RequestMapping(value = "/query/{roleId}", method = RequestMethod.POST)
    public String queryByList(@PathVariable Long roleId){
        List<Long> menus = roleAppService.queryByAppId(roleId);
        return ReturnUtil.returnSucc(menus);
    }
}
